package com.help10000.rms.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.amir.common.api.ApiContents;
import com.amir.common.mvp.base.MBaseAdapter;
import com.amir.common.utils.ImageHelper;
import com.amir.common.utils.LogUtils;
import com.help10000.rms.R;

import java.util.List;


public class ReleaseeFrenPicsdAdapter extends MBaseAdapter<String> {
    public ReleaseeFrenPicsdAdapter(Context context, List<String> list, int maxCount) {
        super(context, list);
        this.maxCount = maxCount;
    }

    private int maxCount = 0;

    @Override
    public int getCount() {
        if (mList.size() == maxCount) {
            return mList.size();
        }
        return mList.size() + 1;
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        LogUtils.Companion.E(i + ":getView" + view == null);
        if (view == null) {
            view = mInflater.inflate(R.layout.item_pic_layout, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if (mList.size() == 9) {
            holder.mIvClose.setVisibility(View.VISIBLE);
            if (mList.get(i).startsWith("file")) {
                ImageHelper.displayImage(mContext, mList.get(i), holder.mIvPic);
            } else
                ImageHelper.displayImage(mContext, ApiContents.ImageUrl + mList.get(i), holder.mIvPic);
            holder.mIvClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mList.remove(i);
                    notifyDataSetChanged();
                }
            });
        } else {
            if (i == mList.size()) {
                LogUtils.Companion.E(i + ":");
                holder.mIvPic.setImageResource(R.mipmap.sczp);
                holder.mIvClose.setVisibility(View.GONE);
            } else {
                LogUtils.Companion.E(i + ":" + mList.get(i));
                if (mList.get(i).startsWith("file")) {
                    ImageHelper.displayImage(mContext, mList.get(i), holder.mIvPic);
                } else
                    ImageHelper.displayImage(mContext, ApiContents.ImageUrl + mList.get(i), holder.mIvPic);
                holder.mIvClose.setVisibility(View.VISIBLE);
                holder.mIvClose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mList.remove(i);
                        notifyDataSetChanged();
                    }
                });
            }
        }


        return view;
    }


    private class ViewHolder {
        public View rootView;
        public ImageView mIvPic;
        public ImageView mIvClose;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.mIvPic = (ImageView) rootView.findViewById(R.id.iv_pic);
            this.mIvClose = (ImageView) rootView.findViewById(R.id.iv_close);
        }

    }
}
